﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using thousandclear.Data;
using thousandClear.Areas.Shop.Models;
using thousandClear.Util.Setting;

namespace thousandClear.Areas.Shop.Controllers
{
    public class MarketOrderController : Controller
    {
        // GET: Shop/MarketOrder
        public ActionResult Index(long? id, string openid)
        {
            OrderModel model = new OrderModel();

            AppSetting setting = new AppSetting();

            using (var _db = new qianjingEntities())
            {
                var record = _db.O_Order_Info.FirstOrDefault(a => a.Id == id);

                if (record != null)
                {
                    decimal amount = record.Amount == null ? 0.00m : record.Amount.Value;
                    //int postFee = record.PostFee == null ? 0 : record.PostFee.Value;

                    model.Id = record.Id;
                    model.SerialNo = record.SerialNo;
                    model.Amount = amount.ToString();
                    //model.PostFee = (postFee / 100.00).ToString("#0.00");
                    model.PostBillNo = record.PostBillNo;
                    
                    model.Status = record.Status;
                    model.OrderTime = record.OrderTime;
                    model.OpenId = openid;
                    model.PayState = record.PayState;

                    model.ReceiveMan = record.ReceiveMan;
                    model.ReceiveMobile = record.ReceiveMobile;
                    model.ReceiveZip = record.ReceiveZip;

                    model.Remark = string.IsNullOrEmpty(record.Remark) ? "N/A" : record.Remark;

                    model.ReceiveAddress = record.ReceiveAddress.ToString();

                    var list = _db.D_Order_list_info.OrderByDescending(a => a.CreatedTime).Where(oo => oo.OrderId == id).ToList();
                    if (list != null)
                    {
                        List<D_Order_list_info> listDD = new List<D_Order_list_info>();
                        foreach (var td in list)
                        {
                            D_Order_list_info doli = new D_Order_list_info();
                            doli = td;
                            doli.GoodImgUrl = string.Format("{0}/{1}/{2}", setting.AppDomainName, "statics/upload/good/image", doli.GoodImgUrl);
                            listDD.Add(doli);
                        }

                        model.GoodItems = listDD;
                    }

                    model.TotalAmount = amount.ToString();
                }
            }

            return View(model);
        }
        [HttpGet]
        public String ReducePoint(long id)
        {
            string returnStr = "";
            using (var _db = new qianjingEntities())
            {
                var record = _db.O_Order_Info.FirstOrDefault(or => or.Id == id);
                var list = _db.D_Order_list_info.OrderByDescending(a => a.CreatedTime).Where(oo => oo.OrderId == id).ToList();

                string openid = record.OpenId;
                var cust = _db.O_Customer.FirstOrDefault(oc => oc.ocOpenid == openid);

                decimal nowPoint = cust.ocGrades.Value;
                decimal buyPoint = (decimal)record.Amount;
                //foreach (var ty in list)
                //{
                //    buyPoint += decimal.Parse(((ty.Amount / 100) * ty.Quantity).ToString());
                //}

                UtilNS.Util.WriteLog("当前积分够还是不够呢？::" + (buyPoint > nowPoint).ToString());

                if (buyPoint > nowPoint)
                {
                    returnStr = "当前积分不够，无法兑换该产品";
                }
                else
                {
                    foreach (var ty in list)
                    {
                        _db.O_Shop_Point.Add(new O_Shop_Point
                        {
                            ospOpenID = openid,
                            ospShopItemDesc = ty.GoodName,
                            ospOrderID = record.Id.ToString(),
                            ospPoint = buyPoint,
                            CreateDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
                        });
                    }

                    cust.ocGrades = cust.ocGrades - buyPoint;

                    record.Status = 1;
                    record.PayState = 1;

                    _db.SaveChanges();

                    returnStr = "1";
                }
            }

            return returnStr;
        }
    }
}